
import paths from './config/path.config'

function routeLoop (list, setter, resolve) {
  list.forEach(r => {
    if (setter[r.name]) Object.assign(r, setter[r.name])
    if (r.children) routeLoop(r.children, setter)
    if (/manager/.test(r.name) && r.children) {
      r.children.push({
        name: 'manager-departList',
        path: '/manager/departList',
        component: resolve(__dirname, 'pages/departs/departList.vue')
      })
    }
  })
}

const {
  api_path_global,
  api_path_local,
  auth_path_local,
  auth_path_global,
  app_path_local,
  app_path_global,
  auth_port_prod,
  auth_port_dev
} = paths
const { api_local = '0', auth_local = '0', app_local = '0', env = 'dev' } = process.env
const isDev = ['prod', 'dev'].indexOf(env)
const API_PATH = [api_path_global, api_path_local][api_local]
const AUTH_PATH = [auth_path_global, auth_path_local][auth_local]
const AUTH_PORT = [auth_port_prod, auth_port_dev][isDev]
const APP_PATH = [app_path_global, app_path_local][app_local]

if (isDev) {
  console.log('==========')
  console.log('AUTH_PORT: ', AUTH_PORT)
  console.log('==========')
}

export default {
  mode: 'spa',

  /*
  ** Headers of the page
  */
  head: {
    title: '虎外OA',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#52c41a' },

  /*
  ** Global CSS
  */
  css: [
    'ant-design-vue/dist/antd.css'
  ],
  styleResources: {
    less: './assets/css/base.less'
  },

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    { src: '@/plugins/antd-ui', ssr: true },
    { src: '@/plugins/nuxt-tools', ssr: true },
    { src: '@/plugins/axios', ssr: true },
    { src: '@/plugins/api' },
    { src: '@/plugins/permissions', ssr: false },
    { src: '@/plugins/vue-tools', ssr: false },
    { src: '@/plugins/formHelper', ssr: false }
  ],

  /*
  ** Nuxt.js dev-modules
  */
  buildModules: [
    '@nuxtjs/eslint-module'
  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    '@nuxtjs/style-resources',
    '@nuxtjs/axios',
    '@nuxtjs/auth'
  ],

  /**
   * auth
   */
  auth: {
    redirect: {
      login: '/users/login',
      logout: '/users/login'
    },
    strategies: {
      local: {
        endpoints: {
          // JWT Login flow (default)
          login: {
            url: `authless/login?scope=read&grant_type=password`,
            method: 'post',
            // propertyName: 'access_token',
            // switch to value with newer version auth, 失败请切换回上方的access_token
            propertyName: 'value',
            auth: {
              username: 'pc_client',
              password: 'test_secret'
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          },
          logout: { url: `auth/logout`, method: 'get' },
          user: { url: `auth/profile`, method: 'get', propertyName: 'data' }
        }
      }
    },
    watchLoggedIn: false,
    plugins: [
      { src: '~/plugins/auth.js', ssr: true }
    ]
  },

  /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
  axios: {
    proxy: true,
    credentials: true
  },

  proxy: {
    '/mock/': {
      target: 'https://b9f69e3a-ac2b-4d38-9a65-63a80dab67de.mock.pstmn.io/',
      changeOrigin: true,
      pathRewrite: {
        '^/mock/': '/'
      }
    },

    '/app/': {
      target: `${APP_PATH}:9999/`,
      changeOrigin: true,
      pathRewrite: {
        '^/app': '/app'
      }
    },

    '/api/': {
      target: `${API_PATH}:18911/`,
      changeOrigin: true,
      pathRewrite: {
        '^/api/': '/'
      }
    },

    '/jy/': {
      target: `${API_PATH}:18910/`,
      changeOrigin: true,
      pathRewrite: {
        '^/jy/': '/'
      }
    },

    // 配置 authless、auth 的请求,用于登录
    '/authless/': {
      target: `${AUTH_PATH}:${AUTH_PORT}/`,
      changeOrigin: true
    },

    '/auth/': {
      target: `${AUTH_PATH}:${AUTH_PORT}/`,
      changeOrigin: true
    },

    // 微服务请求
    '/authentication-server/': {
      target: `${AUTH_PATH}:8443`,
      changeOrigin: true
    },

    // 七牛云
    '/qiniuUpload/': {
      target: 'http://upload-z2.qiniup.com',
      changeOrigin: true,
      pathRewrite: {
        '^/qiniuUpload': ''
      }
    }
  },

  /**
   * router
   */
  router: {
    middleware: ['auth'],
    extendRoutes (routes, resolve) {
      routes.push({
        name: 'home-redirect',
        path: '*',
        component: resolve(__dirname, 'pages/exception/404.vue')
      })
      routeLoop(routes, {
        'meal-count': { path: 'count/:dateStr?', props: true },
        'meal-date-edit': { path: 'edit/:dateStr', props: true },
        'meal-date-detail': { path: 'detail/:dateStr', props: true },
        'property-plan-child': { path: '/property/plan/child/:name/:id', props: true },
        'property-plan-child-add': { path: 'add/:name/:id', props: true },
        'property-plan-child-detail': { path: 'detail/:name/:id/:childName/:childId/:isDetail?', props: true },
        'property-item-detail': { path: 'detail/:id', props: true },
        'property-warehouse-handle': { path: 'handle/:id/:applyType', props: true },
        'staff-StaffIndex': { meta: { keepAlive: false }},
        'staff-StaffSave': { path: '/staff/StaffSave/:id', props: true, meta: { keepAlive: false }},
        'blacklist-BlacklistIndex': { meta: { keepAlive: false }},
        'qrcode': { meta: { keepAlive: false }}
      }, resolve)
    }

  },

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    // cache: true,
    parallel: true,
    hash: Date.now(),
    splitChunks: {
      pages: true,
      commons: true
    },
    maxChunkSize: 30000,
    extend (config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }
}
